﻿using System.Net;

namespace ProxyServer.Acme;

public static class HttpUtil
{
    private static readonly HttpClient _Client = new HttpClient();

    public static HttpClient Client => _Client;

    public static async Task<string?> GetStringAsync(string url)
    {
        var resp = await Client.GetAsync(url);

        if (resp.StatusCode != HttpStatusCode.OK)
        {
            if (resp.StatusCode == HttpStatusCode.NotFound)
                return null;
            throw new Exception("HTTP request error:  "
                    + $"({resp.StatusCode}) {await resp.Content.ReadAsStringAsync()}");
        }

        return await resp.Content.ReadAsStringAsync();
    }
}
